/**
  ******************************************************************************
  * @file    DMA/stm32f10x_it.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    15/09/2010
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and peripherals
  *          interrupt service routine.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */


/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include <string.h>
#include <time.h>
#include <stdio.h>


/** @addtogroup Examples
  * @{
  */



/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//extern __IO uint16_t CurrDataCounterEnd;
extern vu8 TimeDisplay;
extern vu32 timer1;
extern vu32 timer2;

//extern vu16 length ;
extern char NX_R[300];
extern u16 length ;
extern char DATEX_R[100];
extern u8  length_d;
extern char NX_W [255];
extern volatile u8 last_error;

extern  char start_datex;
extern  char end_datex;
extern  char nak;
extern  char syn;
extern  char MFCSB1,MFCSB2;
extern  char ESC ;//= 0x1b;
extern  char PLUS;
extern  char PLUS9;
extern  char PLUS8;
extern  char PLUSA;
extern  char PLUSF;
extern  char MFB;
extern  char MFB1;
extern  char MFB2;
extern  char MFE;

extern char BOOT_MODE;
extern char BOOT_MODE_FIRST;
extern char BOOT_MODE_CONFIRM;


//extern time_t ltime;
//extern struct tm ptim;
//extern char last_get_data [240];
//extern volatile u8 last_error;
//extern volatile char comma;
//extern volatile char commb;




//extern  char   commb;
//extern  char   comma;

//extern struct    command  ;
extern  struct command comm ;
struct  command {
  unsigned  esc: 1;
  unsigned  esc_plus: 1;
  unsigned  esc_plus9:4;
  unsigned  esc_plus8:4;
  unsigned  esc_plusA:4;
  unsigned  esc_plusF:4;
  unsigned  esc_plus_ok:1;
  unsigned  esc_mfb: 1;
  unsigned  esc_mfb1: 1;
  unsigned  esc_mfb2: 1;
  unsigned  esc_mfe: 1;
   unsigned  printing_bon: 1;
   unsigned  boot_active:1;
   unsigned boot_allow:1;
   unsigned boot_mode:1;
   unsigned boot_mode_first:1;
  
};

extern struct read_datex
{
  unsigned start:1;
  unsigned end:1;
  unsigned nak:1;
  unsigned syn:1;
} read_datex ;



/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {

  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}

/******************************************************************************/
/*            STM32F10x Peripherals Interrupt Handlers                        */
/******************************************************************************/

/**
  * @brief  This function handles DMA1 Channel 6 interrupt request.
  * @param  None
  * @retval None
  */
void DMA1_Channel6_IRQHandler(void)
{
  /* Test on DMA1 Channel6 Transfer Complete interrupt */
 // if(DMA_GetITStatus(DMA1_IT_TC6))
  //{
    /* Get Current Data Counter value after complete transfer */
  //  CurrDataCounterEnd = DMA_GetCurrDataCounter(DMA1_Channel6);
    /* Clear DMA1 Channel6 Half Transfer, Transfer Complete and Global interrupt pending bits */
  //  DMA_ClearITPendingBit(DMA1_IT_GL6);
  //}
}

void RTC_IRQHandler (void)
{
  if(RTC_GetITStatus(RTC_IT_SEC) != RESET )
  {
    //Clear the RTC Second interrupt
    RTC_ClearITPendingBit(RTC_IT_SEC);

    //Toggle led connection to PC.09 pin each 1s
    GPIO_WriteBit(GPIOC,GPIO_Pin_9,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_9)));

    //Enable time update

    TimeDisplay =1;
     //Wait until last write operation on RTC register has finished

    RTC_WaitForLastTask();


  }
}


void USART1_IRQHandler(void)
{
  char  usart1_read ;

  //Check for RXNE
  if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET)
  {
     //Clear pending bit
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);

    //Read data from COM1
    usart1_read=USART_ReceiveData(USART1);
    
   //asm("nop");
    
      /* Waiting for second Confirm char Set Boot_Mode Active else reset Special char */
       if (comm.boot_mode_first)
       {      
         if(usart1_read == BOOT_MODE_CONFIRM) comm.boot_active=1;
          else comm.boot_active=0;
       }
               
       /* Waiting for first Confirm char else reset Special char */   
      if(comm.boot_mode)
      {
        if(usart1_read == BOOT_MODE_FIRST) comm.boot_mode_first=1; 
      }
      else
      {
        comm.boot_mode=0;
        comm.boot_mode_first=0;
      }
          /* Waiting for Special Char for Boot Mode */   
       if(usart1_read == BOOT_MODE) comm.boot_mode=1;
        else comm.boot_mode=0;
      
  
    
    /* Waiting for second Confirm char Set Boot_Mode Active else reset Special char */






   if (comm.esc_mfb)
    {
    //Save data to NX_R
    NX_R[length++]=usart1_read;


   // Watch NX_R size
    if (length > 296) length=0;

    }
   //Watch for ESC command

   /*
      if (comm.esc_mfb ==1 & comma !=0 & commb==0)  //Save the second symvol Only Esc_MFB12
      {
        commb=usart1_read;
      }
   */


      if (comm.esc_plus  )  //comm.esc_mfb ==1 Save Next symvol after esc+ or esc_mfb command && comma==0
      {
       // comma=usart1_read;
        if (usart1_read == PLUSA)
        {
         comm.esc_plusA=1;
         comm.esc_plus_ok=1;
        }
           /*
            //Mfc_Info ();
             //comm.esc_plusA=0; //--
             comm.esc_plus=0;
             comm.esc_plus_ok=0;
             //printf("\r\nESC+A");
         //      printf("0A,");
             //char prm [80];
             struct tm ytim ;
             ltime=RTC_GetCounter() +0x3FF36300 ;
             ptim=*localtime(&ltime);
             ltime=RTC_GetCounter() +0x3FF36300 -86400L ; //Yestar Day time
             ytim=*localtime(&ltime);

             sprintf(NX_W,"%cs%c%c%c%c%c;%2.2d,%2.2d,%2.2d;%2.2d,%2.2d,%2.2d;%6d;%2.2d,%2.2d,%2.2d;BN00114951001263    103533691;%c",
             ESC,0,68,0x0A,MFCSB1,MFCSB2,
             ptim.tm_sec,ptim.tm_min,ptim.tm_hour,
             ptim.tm_mday,ptim.tm_mon+1,ptim.tm_year -100,120,
             ytim.tm_mday,ytim.tm_mon,ytim.tm_year-100,7);

             //USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

             //Send_NX();
              u16 i;
             // USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

             //DATEX_W NX_R//
             for (i=0;NX_W[i]!=7;i++)
            {
              USART_SendData(USART1,(u8) NX_W[i]);
               while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ;;//Wait for send data
            }
            // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
   //printf("\r\nSend Usart1");

            // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
           */

         else if (usart1_read ==PLUS8)
         {
             //  comm.esc_plus8=1;

             // if(comma=='i' && commb=='i')last_error=0x31;
             // Mfc_Sync();
              //comm.esc_plus8=0; //--

              //printf("\r\nESC+8");
             // printf("08,");
              //Status_Datex();
           /*    from 23.11.2011
               sprintf(NX_W,"%c%s%c%c%c%c%c%c",ESC,"s",0,3,8,0x60,last_error,7);  //Add 7 for end of message 0xf0 ~ 0x60

             // Send_NX();
               u16 i;
             // USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

             //DATEX_W NX_R//
             for (i=0;NX_W[i]!=7;i++)
            {
              USART_SendData(USART1, NX_W[i]);
               while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ;;//Wait for send data
            }
            // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
           */


              comm.esc_plus8=1;
             // comm.esc_plus=1;
              comm.esc_plus_ok=1;
         }

          else if(usart1_read ==PLUS9)
          {
            comm.esc_plus9=1;
            comm.esc_plus_ok=1;
          }

          /*
             {
           // Mfc_Test();
           //comm.esc_plus9=0;//--
           comm.esc_plus=0;
           comm.esc_plus_ok=0;
           //printf("\r\nESC+9");
           //printf("09,");

           sprintf(NX_W,"%c%s%c%c%c%s%c",
           ESC,"s",0,strlen(last_get_data)+1,9,last_get_data,7 );
           //Send_NX();
            u16 i;
            //  USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

             //DATEX_W NX_R//
             for (i=0;NX_W[i]!=7;i++)
            {
              USART_SendData(USART1,(u8) NX_W[i]);
               while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ;;//Wait for send data
            }
            // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

           }
          */


           else if (usart1_read == PLUSF)
           {
            comm.esc_plusF=1;
           comm.esc_plus_ok=1;
           }
       // commb=0;
          }


      if (comm.esc==1)
      {
       if (usart1_read == PLUS)
         {
          comm.esc_plus=1;
          comm.esc = 0;
          //comma=0;
         }
           else if (usart1_read == MFB)
           {
         comm.esc_mfb=1;
         comm.esc_mfe=0;
         comm.esc=0;
         //comma=0;
         //commb=0;
           }
          else if (usart1_read == MFE)
         {
           comm.esc_mfe=1;
           comm.esc=0;
           NX_R[length++]=0x00; // end of string
           NX_R[length++]=0x00; // end of string
          // NX_R[length++]=0x00; // end of string
          // NX_R[length++]=0x00; // end of string
          // com.esc_mfb=0;
         }

       /*
         else if (usart1_read == MFB1)
         {
          comm.esc_mfb1=1;
          comm.esc=0;
         }

       */

      }

       comm.esc=0;  //Don't find command with ESC

      if (usart1_read == ESC)
         {
           comm.esc=1;
         }

      }
    }


    // USART_SendData(USART1,usart1_read); //Echo Talk





void USART2_IRQHandler(void)
{
  char datar2;

  if(USART_GetITStatus(USART2,USART_IT_RXNE)!= RESET)
  {
     //Clear pending bit
    USART_ClearITPendingBit(USART2, USART_IT_RXNE);

    //Read data from COM1
    datar2=USART_ReceiveData(USART2);

      if (datar2 == start_datex) read_datex.start=1 ;
       else if (datar2==nak) read_datex.nak=1;
        else if (datar2==syn) read_datex.syn=1;


       if (read_datex.start ==1)
       {
         DATEX_R[length_d++]=datar2;
          if (length_d > 100) length_d=0;
          if (datar2 == end_datex)
          {
            read_datex.start=0 ;
            read_datex.end=1;
            length_d=0;
          }
       }
    }
 }

/* Timer 2 IRQ Handler*/

void TIM2_IRQHandler(void)

{
  if (TIM_GetITStatus(TIM2,TIM_IT_Update) !=RESET)
  {
       /* Clear Timer Update Status Bit */
      TIM_ClearITPendingBit(TIM2,TIM_IT_Update);

        /*Increment timer1 */
      timer1++;
      timer2++;
  }
}



void TIM3_IRQHandler(void)

{
  if (TIM_GetITStatus(TIM3,TIM_IT_Update) !=RESET)
  {
       /* Clear Timer Update Status Bit */
      TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
      GPIO_WriteBit(GPIOC,GPIO_Pin_7,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_7)));

  }
}


















/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
